package cn.wangfeixiong.mapper;

import cn.wangfeixiong.model.dto.OrderDTO;
import cn.wangfeixiong.model.entity.OrderDO;
import cn.wangfeixiong.model.vo.OrderVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants.ComponentModel;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * spring @Autowired
 */
//@Mapper(componentModel = ComponentModel.SPRING)
public interface OrderMapper2 {

    OrderMapper2 MAPPER = Mappers.getMapper(OrderMapper2.class);


    /**
     * 一对一简单映射
     *  1. 映射条件 属性名相同，有get set方法
     *  2. 类型不同会默认做类型转化
     *
     * @param orderDO
     * @return
     */
    OrderDTO convert(OrderDO orderDO);


    /**
     * 一对一类型映射
     * @param orderDO
     * @return
     */
    @Mappings(
            value ={
                    @Mapping(source="productName",target = "goodsName"),
                    @Mapping(source="createTime",target = "createTime",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(source="updateTime",target = "updateTime",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(target = "buyNum",expression = "java(java.lang.String.valueOf(orderDO.getBuyNum()))")
            }
    )
    OrderVO convert2(OrderDO orderDO);


    /**
     * 一对多类型映射
     * @param orderDO
     * @return
     */
    @Mappings(
            value ={
                    @Mapping(source="orderDO.productName",target = "goodsName"),
                    @Mapping(source="orderDO.createTime",target = "createTime",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(source="orderDO.updateTime",target = "updateTime",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(source="orderDO.orderNo",target = "orderNo",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(source="orderDO.orderType",target = "orderType"),
                    @Mapping(target = "orderDO.buyNum",expression = "java(java.lang.String.valueOf(orderDO.getBuyNum()))")
            }
    )
    OrderVO convert3(OrderDO orderDO,OrderDTO orderDTO);


    /**
     *  不创建新对象属性映射
     * @param orderDO
     * @return
     */
    void convert4(@MappingTarget  OrderDTO orderDTO, OrderDO orderDO);

    /**
     *  自己写转化
     * @param orderDO
     * @return
     */
    default void convert5(OrderDTO orderDTO, OrderDO orderDO){

    }
}
